package Socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 何惠民
 */
public class Server {
    private ServerSocket serverSocket;
    //存放所有客户端输出流用于广播消息
    private List<PrintWriter> allOut = new ArrayList<>();
    public Server(){
        try {
            System.out.println("正在启动服务端...");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务端启动完毕!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start(){
        try {
            while(true) {
                System.out.println("等待客户端连接...");
                Socket socket = serverSocket.accept();
                System.out.println("一个客户端连接了!");
                //启动一个线程处理与该客户端的交互
                ClientHandler handler = new ClientHandler(socket);
                Thread t = new Thread(handler);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;//记录该客户端的IP地址信息

        public ClientHandler(Socket socket) {
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();
        }
        public void run(){
            PrintWriter pw = null;
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                //通过socket获取输出流给客户端回复消息
                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);
                //将改输出流存入共享集合allOut中
                synchronized (allOut){
                    allOut.add(pw);
                }
                System.out.println(host+"上线了，当前在线人数:"+allOut.size());

                String message;
                while((message = br.readLine())!=null){
                    System.out.println(host + "说:"+message);
                    //将消息回复给客户端
                    synchronized (allOut){
                        for (PrintWriter o: allOut) {
                            o.println(host+"说:"+message);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    pw.close();
                    synchronized (allOut){
                        allOut.remove(pw);
                    }
                    System.out.println(host+"下线了，当前在线人数:"+allOut.size());
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
